import { createSlice } from "@reduxjs/toolkit";
import {
  getCrumbs,
  getPrePath,
  setCrumbs,
  setPrePath,
} from "../../sessionStorage/sessionStorageMgr.jsx";
import routers from "../../../routers/routes.jsx";
import url from "../../url";

const crumbsSlice = createSlice({
  name: "record",
  initialState: {
    crumbs: getCrumbs() || [],
  },
  reducers: {
    set_crumbs(state, action) {
      if (!(action.payload instanceof Array)) {
        return;
      }
      state.crumbs = action.payload;
      setCrumbs(state.crumbs);
    },
    push_crumbs(state, action) {
      if (!action.payload) {
        return;
      }
      var crumb = state.crumbs.find((d) => d.name === action.payload.name);
      if (crumb) {
        crumb = action.payload;
        return;
      }
      state.crumbs.push(action.payload);
      setCrumbs(state.crumbs);
    },
    remove_crumbs(state, action) {
      if (!action.payload) {
        return;
      }
      state.crumbs = state.crumbs.filter((d) => d.path != action.payload);
      setCrumbs(state.crumbs);
    },
  },
});

const { push_crumbs, remove_crumbs, set_prePath } = crumbsSlice.actions;

// 实现业务逻辑
const getCurrentRouteList = (currentPath, routeTrees) => {
  var currentPaths = currentPath.split("/");
  return _getCurrentRouteList(currentPaths, routeTrees);
};
const _getCurrentRouteList = (currentPaths, routeTrees, deep = 0) => {
  // 如果没有子级
  if (!routeTrees) {
    return [];
  }
  // 如果深度超过了路径的长度就不可能匹配上
  if (deep > currentPaths.length) {
    return [];
  }
  // 循环当前深度的route数组
  for (let i = 0; i < routeTrees.length; i++) {
    const routeTree = routeTrees[i];
    // 获取能够匹配的路径即去掉"/"
    const path = routeTree.path
      ? routeTree.path.startsWith("/")
        ? routeTree.path.substring(1)
        : routeTree.path
      : "";
    // 判断是否相等或者是:参数路径
    // console.log(path.toLowerCase(), currentPaths[deep]);
    if (
      url.isSamePath(path.toLowerCase(), currentPaths[deep]) ||
      path.toLowerCase().startsWith(":")
    ) {
      // 如果为空则追加一次判断
      if (path.toLowerCase() !== "") {
        return [
          routeTree,
          ..._getCurrentRouteList(currentPaths, routeTree.children, deep + 1),
        ];
      } else {
        currentPaths.push("");
        return [
          routeTree,
          ..._getCurrentRouteList(currentPaths, routeTree.children, deep + 1),
        ];
      }
    }
  }
  return [];
};

// 加入根据路由
const push_crumb_by_location = (location) => {
  return (dispatch) => {
    var currentRouteList = getCurrentRouteList(location.pathname, routers);
    console.log(currentRouteList);
    if (currentRouteList && currentRouteList.length > 0) {
      dispatch(
        push_crumbs({
          path: location.pathname,
          name: currentRouteList[currentRouteList.length - 1].name,
        })
      );
    }
  };
};

// 移除根据路径
const remove_crumb_by_path = (path) => {
  return (dispatch) => {
    dispatch(remove_crumbs(path));
  };
};

export { push_crumb_by_location, remove_crumb_by_path };

export default crumbsSlice;
